const formatTime = date => {
	const year = date.getFullYear()
	const month = date.getMonth() + 1
	const day = date.getDate()
	const hour = date.getHours()
	const minute = date.getMinutes()
	const second = date.getSeconds()

	return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}
/**
 * @desc: 格式化数字
 * @return: n > 10 [eg: 12] => 12 | n < 10 [eg: 3] => '03'
 * @param {*} n
 */

const formatNumber = n => {
	n = n.toString()
	return n[1] ? n : '0' + n
}

const wxuuid = function() {
	var s = [];
	var hexDigits = "0123456789abcdef";
	for (var i = 0; i < 36; i++) {
		s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
	}
	s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
	s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
	s[8] = s[13] = s[18] = s[23] = "-";

	var uuid = s.join("");
	return uuid
}


function js_date_time(value) {
	console.log(value)
	if (!value) return '';
	var now = new Date(value);
	console.log("time:" + JSON.stringify(now));
	var year = now.getFullYear();
	var month = now.getMonth() + 1;
	if (month < 10) {
		month = '0' + month
	}
	var date = now.getDate();
	if (date < 10) {
		date = '0' + date
	}
	return year + "-" + month + "-" + date
}

// 富文本工具类
const formatRichText = (html) => {
	let newContent = html.replace(/<img[^>]*>/gi, function(match, capture) {
		match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
		match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
		match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
		return match;
	});
	newContent = newContent.replace(/style="[^"]+"/gi, function(match, capture) {
		match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi, 'max-width:100%;');
		return match;
	});
	newContent = newContent.replace(/<br[^>]*\/>/gi, '');
	newContent = newContent.replace(/\<img/gi,
		'<img style="max-width:100%;height:auto;display:block;margin-top:0;margin-bottom:0;"');


	//判断img是否存在
	if (newContent.indexOf('img') != -1) {
		var result = newContent.replace(/<img [^>]*src=['"]([^'"]+)[^>]*>/gi, function(match, capture) {
			// return '<img src=' + config().api + capture +
			// 	' style="max-width:100%;height:auto;display:block;margin-top:0;margin-bottom:0;"/>';
			return '<img src=' + capture +
				' style="max-width:100%;height:auto;display:block;margin-top:0;margin-bottom:0;"/>';
		});
		return result;
	}


	return newContent;
}
const isWxin = function() {
	var ua = window.navigator.userAgent.toLowerCase();
	if (ua.match(/MicroMessenger/i) == 'micromessenger') {
		return true
	} else {
		return false
	}
}
//判断机型Android还是ios
const isAndroid = function() {
	var ua = navigator.userAgent,
		app = navigator.appVersion;
	var isAndroid = ua.indexOf('Android') > -1 || ua.indexOf('Linux') > -1;
	var isIOS = !!ua.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
	if (isAndroid) {
		// 安卓设备
		return true
	}
	if (isIOS) {
		// 苹果设备
		return false
	}
}
const config = function() {
	let api = ""
	if (process.env.NODE_ENV === 'development') {
		// 本地 开发环境
		api = 'http://192.168.0.68:8092/dev'
		// api = 'http://web.52yuze.com/dev'
	} else {
		// 线上 生产环境
		api = 'http://web.52yuze.com/dev'
	}
	return {
		api: api,
	}
}

module.exports = {
	config: config,
	formatRichText: formatRichText,
	isWxin: isWxin,
	isAndroid:isAndroid
}
